package wolf.opensource.lcre.util;

import java.util.Iterator;
import java.util.Map;

import org.apache.velocity.VelocityContext;

public class VelocityHelper {
	
	public static String eval(String input, Map map) throws Exception {
		return SimpleVelocityEngine.getUniqueInstance().evaluate(input, makeContext(map));
	}

	public static String eval(String input, String key, Object obj) throws Exception {
		VelocityContext context = new VelocityContext();
		context.put(key, obj);
		return SimpleVelocityEngine.getUniqueInstance().evaluate(input, context);
	}

	public static String eval(String input, VelocityContext context) throws Exception {
		return SimpleVelocityEngine.getUniqueInstance().evaluate(input, context);
	}

	public static VelocityContext makeContext(Map map) {
		VelocityContext context = new VelocityContext();
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			context.put(entry.getKey().toString(), entry.getValue());
		}
		return context;
	}

	public static VelocityContext makeContext(Map map, boolean lowercaseFirstLetter) {
		VelocityContext context = new VelocityContext();
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			context.put(entry.getKey().toString(), entry.getValue());
		}
		return context;
	}

	public static void fullContext(VelocityContext context, Map map) {
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			context.put(entry.getKey().toString(), entry.getValue());
		}
	}
}
